package org.yoao.crm.struts.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.yoao.crm.bean.StaticBean;
import org.yoao.crm.business.impl.CompanyServiceImpl;
import org.yoao.crm.business.impl.RoleServiceImpl;
import org.yoao.crm.business.impl.UserServiceImpl;
import org.yoao.crm.business.service.ICompanyService;
import org.yoao.crm.business.service.IRoleService;
import org.yoao.crm.business.service.IUserService;
import org.yoao.crm.struts.form.UserForm;

/**
 * @author yxwonline.com
 * @description 
 * @date 2010-8-4 上午12:18:56
 */
public class RegeditServlet extends HttpServlet {

	public RegeditServlet() {
		super();
	}

	@Override
	public void init() throws ServletException {
		
	}
	
	@Override
	public void destroy() {
		super.destroy();		
	}

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}
	
	private IUserService userService = new UserServiceImpl();
	private ICompanyService companyService = new CompanyServiceImpl();
	private IRoleService roleService = new RoleServiceImpl();
	
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String method = request.getParameter("method");
			if("regedit".equals(method)){
				response.setCharacterEncoding("GBK");
				PrintWriter out = response.getWriter();
				String result = null;
				String info = regedit(request);
				System.out.println(info);
				if(info.indexOf("成功") > -1)
					result = "<script>alert('注册成功,请等待审核!');window.close();</script>";
				else if(info.indexOf("失败") > -1)
					result = "<script>alert('注册失败!');history.back();</script>";
				else
					result = "<script>alert('"+info+"!');history.back();</script>";
				out.println(result);
				out.flush();
				out.close();
				return;
			}else{
				request.setAttribute("ROLECHILDS", roleService.getAllChildRole(StaticBean.ROLE_HQ));
				request.setAttribute("COMPANY", companyService.getCompanys());
			}
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("Message", "系统异常,暂不能注册新用户,请与管理员联系.");
		}
		request.getRequestDispatcher("/regedit.jsp").forward(request, response);
	}
	
	private String regedit(HttpServletRequest request){		
		try {
			String userName = request.getParameter("userName");
			String nickName = request.getParameter("nickName");
			String passWord = request.getParameter("passWord");
			String role = request.getParameter("role");
			String phone = request.getParameter("phone");
			String address = request.getParameter("address");
			String companyId = request.getParameter("companyId");
			String position = request.getParameter("position");
			
			UserForm userForm = new UserForm();
			userForm.setUserName(userName);
			userForm.setNickName(nickName);
			userForm.setPassWord(passWord);
			userForm.setRole(role);
			userForm.setCompanyId(companyId);
			userForm.setPhone(phone);
			userForm.setAddress(address);
			userForm.setPosition(position);
			userForm = userService.add(userForm);
			return userForm.getInfo();			
		} catch (Exception e) {
			e.printStackTrace();
			return "注册失败";
		}
	}
}
